<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs>
          <el-tab-pane class="tree-card" label="组织架构">
            <tree-Tools :node-data="company" :is-root="true" @addDepart="addDepart" />
            <hr>
            <!-- 树状菜单 -->
            <el-tree :data="departments" :props="defaultProps" default-expand-all>
              <template v-slot="{ data }">
                <tree-tools
                  :node-data="data"
                  @getDepartments="getDepartmentList"
                  @addDepart="addDepart"
                  @editDepart="editDepart"
                />
              </template>
            </el-tree>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <add-dept
        ref="add"
        :show-dialog="showDialog"
        :node="currentNode"
        @close="showDialog = false"
        @getDepartments="getDepartmentList"
      />
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools'
import AddDept from './components/AddDept'
import { getDepartments } from '@/api/departments'
import { listTOTree } from '@/utils'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departments: [
      ],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客',
        manager: '负责人',
        id: ''
      },
      // 是否显示loading效果
      loading: false,
      showDialog: false,
      currentNode: {}
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.loading = true
      // 发送请求,获取部门数据
      const res = await getDepartments()
      console.log(res)
      // 修改数据
      this.departments = listTOTree(res.depts)
      this.company.name = res.companyName
      this.loading = false
    },
    addDepart(nodeData) {
      this.showDialog = true
      this.currentNode = nodeData
    },
    // 修改部门
    editDepart(nodeData) {
      this.showDialog = true
      this.currentNode = nodeData
      // 回显
      this.$refs.add.getDepartDetail(nodeData.id)
    }
  }
}
</script>

<style  lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
